package com.runen.infinitemovement.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.MyHomeActivityV2;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.entity.RerverLikeEntity;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;

public class RerverLikeAdapter extends BaseQuickAdapter<RerverLikeEntity.DataBean, BaseViewHolder> implements LoadMoreModule {
    private Context context;

    public OnItemFocusClickListener getOnItemFocusClickListener() {
        return onItemFocusClickListener;
    }

    public void setOnItemFocusClickListener(OnItemFocusClickListener onItemFocusClickListener) {
        this.onItemFocusClickListener = onItemFocusClickListener;
    }

    private OnItemFocusClickListener onItemFocusClickListener;

    public RerverLikeAdapter(Context context) {
        super(R.layout.item_my_focus);
        this.context = context;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, RerverLikeEntity.DataBean item) {
        RoundedCornersImage rci_user_header = helper.getView(R.id.rci_user_header);
        TextView tv_name = helper.getView(R.id.tv_name);
        TextView tv_focus = helper.getView(R.id.tv_focus);
        GlideUtils.loadImage(context, item.headImgUrl, rci_user_header);
        tv_name.setText(item.nickname);
        if (!TextUtils.isEmpty(item.userId) && !TextUtils.isEmpty(UserAPI.getUserId()) && UserAPI.getUserId().equals(item.userId)) {
            tv_focus.setVisibility(View.GONE);
        } else {
            tv_focus.setVisibility(View.VISIBLE);
        }
        if (item.isFollow) {
            tv_focus.setText("已关注");
            tv_focus.setTextColor(CommonUtil.getColor(R.color.color_FFED3F37));
            tv_focus.setBackground(CommonUtil.getDrawable(R.drawable.bg_ed3f37_dp360));
        } else {
            tv_focus.setText("关注");
            tv_focus.setBackground(CommonUtil.getDrawable(R.drawable.bg_fa5438_f63d5b_dp360));
            tv_focus.setTextColor(CommonUtil.getColor(R.color.white));
        }
        tv_focus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取消关注
                if (item.isFollow) {
                    if (null != onItemFocusClickListener) {
                        item.isFollow = false;
                        tv_focus.setText("已关注");
                        tv_focus.setTextColor(CommonUtil.getColor(R.color.color_FFED3F37));
                        tv_focus.setBackground(CommonUtil.getDrawable(R.drawable.bg_ed3f37_dp360));
                        onItemFocusClickListener.cancelFocus(item, helper.getAdapterPosition());
                    }
                } else {
                    if (null != onItemFocusClickListener) {
                        item.isFollow = true;
                        tv_focus.setText("关注");
                        tv_focus.setTextColor(CommonUtil.getColor(R.color.white));
                        tv_focus.setBackground(CommonUtil.getDrawable(R.drawable.bg_fa5438_f63d5b_dp360));
                        onItemFocusClickListener.addFocus(item, helper.getAdapterPosition());
                    }
                }

            }
        });

        rci_user_header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MyHomeActivityV2.class);
                intent.putExtra("userId", item.userId);
                context.startActivity(intent);
            }
        });
    }

    public interface OnItemFocusClickListener {
        /**
         * 添加关注
         *
         * @param item
         */
        void addFocus(RerverLikeEntity.DataBean item, int position);

        void cancelFocus(RerverLikeEntity.DataBean item, int position);
    }
}
